package com.example.wechattakeaway.service.Impl;

import com.example.wechattakeaway.entity.DishOption;
import com.example.wechattakeaway.entity.OptionDetail;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.vo.AllOptionVo;
import com.example.wechattakeaway.form.meroptionsform.InsertDishOptionForm;
import com.example.wechattakeaway.form.meroptionsform.UpdateDishOptionForm;
import com.example.wechattakeaway.mapper.DishOptionMapper;
import com.example.wechattakeaway.mapper.OptionDetailMapper;
import com.example.wechattakeaway.service.DishOptionService;
import com.example.wechattakeaway.utils.PageUtil;
import com.example.wechattakeaway.utils.ResultVOUtil;
import com.example.wechattakeaway.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Repository
public class DishOptionServiceImpl implements DishOptionService {

    @Autowired
    DishOptionMapper dishOptionMapper;

    @Autowired
    OptionDetailMapper optionDetailMapper;

    @Override
    public ResultVO insertDishOption(InsertDishOptionForm insertDishOptionForm) {
        DishOption dishOption=new DishOption();
        dishOption.setOptionName(insertDishOptionForm.getOptionName());
        dishOption.setDishId(insertDishOptionForm.getDishId());

        try {

            dishOptionMapper.insert(dishOption);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }

    @Override
    public ResultVO updateDishOption(UpdateDishOptionForm updateDishOptionForm) {
        DishOption dishOption=new DishOption();
        dishOption.setOptionName(updateDishOptionForm.getOptionName());
        dishOption.setDishOptionId(updateDishOptionForm.getDishOptionId());

        try {

            dishOptionMapper.updateByPrimaryKeySelective(dishOption);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }

    @Override
    public ResultVO selectOptionById(Integer dishOptionId) {
        DishOption dishOption=new DishOption();
        try {

            dishOption=dishOptionMapper.selectByPrimaryKey(dishOptionId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success(dishOption);

    }

    @Override
    public ResultVO selectOptionByDishId(Integer pageNum,Integer dishId) {
        List<DishOption> list=new ArrayList<>();

        try {
            PageHelper.startPage(pageNum,5);
            list= dishOptionMapper.selectByDishId(dishId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap = PageUtil.getMap(list);

        return ResultVOUtil.success(hashMap);
    }

    @Override
    public ResultVO deleteDishOption(Integer optionId) {
        try {

            dishOptionMapper.deleteByPrimaryKey(optionId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }

    @Override
    public ResultVO selectAllById(Integer dishOptionId) {

        DishOption dishOption=new DishOption();
        OptionDetail optionDetail=new OptionDetail();
        List<OptionDetail> optionDetails=new ArrayList<>();
        AllOptionVo allOptionVo=new AllOptionVo();
        try {
            optionDetails=optionDetailMapper.selectByOptionId(dishOptionId);
            dishOption=dishOptionMapper.selectByPrimaryKey(dishOptionId);
            allOptionVo.setDishOptionId(dishOptionId);
            allOptionVo.setDishId(dishOption.getDishId());
            allOptionVo.setOptionName(dishOption.getOptionName());
            List<Integer> ids=new ArrayList<>();
            List<String> details=new ArrayList<>();
            for(int i=0;i<optionDetails.size();i++){
                ids.add(optionDetails.get(i).getOptionDetailId());
            }
            for(int i=0;i<optionDetails.size();i++){
                details.add(optionDetails.get(i).getOptionDetail());
            }
            allOptionVo.setOptionDetailIds(ids);
            allOptionVo.setOptionDetails(details);


        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success(allOptionVo);


    }

    @Override
    public ResultVO selectByDishId(Integer dishId) {
        List<DishOption> list=new ArrayList<>();

        try {
          //  PageHelper.startPage(pageNum,5);
            list= dishOptionMapper.selectByDishId(dishId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
       // hashMap = PageUtil.getMap(list);
        hashMap.put("list",list);

        return ResultVOUtil.success(hashMap);
    }
}
